use crate::{common, tables::attend::TbAttend};
use actix_web::{get, web, Responder};

#[get("")]
async fn get(page: web::Query<common::QueryPage>) -> impl Responder {
    common::ResData::<TbAttend>::from_page(TbAttend::get_attends(&page).await).resp_json()
}

pub fn routes(cfg: &mut web::ServiceConfig) {
    cfg.service(web::scope("/attend").service(get));
}

#[cfg(test)]
pub mod tests {
    use crate::{handlers::attend, utils::mysql};
    use actix_web::{test, App};
    use serde_json::Value;

    #[actix_web::test]
    async fn test_get_attends() {
        mysql::init_pool().await;

        let app = test::init_service(App::new().configure(attend::routes)).await;

        let req = test::TestRequest::get().uri("/attend").to_request();

        let resp = test::call_service(&app, req).await;
        let status = resp.status();
        println!("resp status is {:?}", status);
        let resp_body = test::read_body(resp).await;
        let json_value: Value = serde_json::from_slice(&resp_body).unwrap();
        println!("restp body is {}", json_value);
        assert!(status.is_success());
    }
}
